import { resolve } from 'path';
import { UserConfigExport, ConfigEnv, loadEnv } from 'vite';
import { configPluginList } from './build/plugins';
import { warpperEnv } from './build/index';

/** 当前执行node命令时文件夹的地址（工作目录） */
const root: string = process.cwd();

/** 路径查找 */
const pathResolve = (dir: string): string => resolve(__dirname, '.', dir);

/** 设置别名 */
const alias: Record<string, string> = {
  '@': pathResolve('src'),
  '@build': pathResolve('build'),
};

/**
 * @description:
 * @param {ConfigEnv} configEnv
 * @return {*}
 */
export default function (configEnv: ConfigEnv): UserConfigExport {
  const viteEnv = warpperEnv(loadEnv(configEnv.mode, root));

  return {
    base: viteEnv.VITE_PUBLIC_PATH,
    root,
    resolve: {
      alias,
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@use "@fssc/admin/src/styles/element/index.scss" as *;',
        },
      },
    },
    plugins: configPluginList(configEnv, viteEnv),
    server: {
      // 端口号
      port: viteEnv.VITE_PORT,
      host: true,
      proxy: {
        [viteEnv.VITE_PROXY_PREFIX]: {
          target: viteEnv.VITE_PROXY,
          changeOrigin: true,
          rewrite: (path) => path.replace(viteEnv.VITE_PROXY_PREFIX, ''),
        },
      },
    },
  };
}
